package com.yd.mvc.baseController.util;


import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;




public class ValidateUtils {

	private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
	/**
	 *验证对象
	 */
	public static String validate(Object obj){
		if(obj==null){
			return "参数不能为空";
		}
		//StringBuilder sb=new StringBuilder();
		Set<ConstraintViolation<Object>> violations = validator.validate(obj);
		if(violations==null||violations.isEmpty()) return null;
		StringBuilder sb=new StringBuilder();
		for(ConstraintViolation<Object> cv:violations){
			sb.append(cv.getMessage()).append("   ");
		}
		return sb.toString();
	}
}
